package com.crm.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.crm.common.annotation.Log;
import com.crm.common.controller.DateUtils ;
import com.crm.common.enums.BusinessType;
import com.crm.common.result.PageResult;
import com.crm.common.result.AjaxResult;
import com.crm.domain.Contract;
import com.crm.dto.CommonStatisticsPageQueryDto;
import com.crm.dto.ContractStatisticsPageQueryDto;
import com.crm.dto.Vo.*;
import com.crm.service.StatsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/report")
public class StatsController {
    @Autowired
    private StatsService statsService;

    /**
     * 客户统计-新增客户端
     */
    @GetMapping("/contractStatistics/{beginCreateTime}/{endCreateTime}")
    @Log(title = "客户统计-新增客户端", businessType = BusinessType.EXPORT)
    private ReportStatisticsVo cunstoneCount(
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginCreateTime,
            @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endCreateTime) {
        log.info("客户统计-新增客户端,参数：{}-{}", beginCreateTime, endCreateTime);
        //将LocaDate转换为LocalDateTime
        //.atStartOfDay() 是 Java 8 LocalDate 的方法，表示当天的 00:00:00。
        LocalDateTime beginDateTime = beginCreateTime.atStartOfDay();
        //传入 LocalDateTimeUtil.endOfDay(...) 方法，这个方法是 Hutool 工具类提供的：
        //它会将时间设置为当天的 最后一秒，即 23:59:59
        LocalDateTime endDateTime = LocalDateTimeUtil.endOfDay(endCreateTime.atStartOfDay());
        return statsService.contractStatistics(beginDateTime, endDateTime, beginCreateTime, endCreateTime);
    }

    /**
     * 客户统计-客户统计列表
     *
     * @return
     */
    @GetMapping("/contractStatisticsList")
    @Log(title = "客户统计-客户统计列表", businessType = BusinessType.EXPORT)
    public CommonPageQueryVo<Contract> contractStatisticsList(ContractStatisticsPageQueryDto contractStatisticsPageQueryDto) {
        log.info("客户统计-客户统计报表,参数：{}", contractStatisticsPageQueryDto);
        // 将日期时间字符串转换为LocalDateTime
        String beginCreateTime = contractStatisticsPageQueryDto.getBeginCreateTime();
        if (StringUtils.hasText(beginCreateTime)) {
            contractStatisticsPageQueryDto.setBeginTime(DateUtils.convertStringToLocalDateTime(beginCreateTime));
        }
        String endCreateTime = contractStatisticsPageQueryDto.getEndCreateTime();
        if (StringUtils.hasText(endCreateTime)) {
            LocalDateTime localDateTime = DateUtils.convertStringToLocalDateTime(endCreateTime);
            contractStatisticsPageQueryDto.setEndTime(LocalDateTimeUtil.endOfDay(localDateTime));
        }
        return statsService.contractPageQuery(contractStatisticsPageQueryDto);
    }


    @GetMapping("/deptStatisticsList/{beginCreateTime}/{endCreateTime}")
    @Log(title = "销售统计-客户统计列表", businessType = BusinessType.EXPORT)
    public CommonPageQueryVo<OwnerShipStatisticsVo> deptStatisticsList(
            @PathVariable String beginCreateTime,
            @PathVariable String endCreateTime,
            CommonStatisticsPageQueryDto dto) {
        log.info("销售统计-客户统计报表");
        // 将日期时间字符串转换为LocalDateTime
        if (StringUtils.hasText(beginCreateTime)) {
            dto.setBeginTime(DateUtils.convertStringToLocalDateTime(beginCreateTime));
        }
        if (StringUtils.hasText(endCreateTime)) {
            LocalDateTime localDateTime = DateUtils.convertStringToLocalDateTime(endCreateTime);
            dto.setEndTime(LocalDateTimeUtil.endOfDay(localDateTime));
        }
        return statsService.deptPageQuery(dto);
    }


}














